<?php

namespace App\Http\Resources\Article;

use App\Http\Resources\BaseResource;
use App\Models\Article\ArticleCategoryModel;
use Illuminate\Http\Resources\MissingValue;

class ArticleCategoryRelListResource extends BaseResource
{
    public function toArray($request): array
    {
        return [
            "id"            => $this->id,
            "articleId"     => $this->articleId,
            "categoryId"    => $this->categoryId,
            "sort"          => $this->sort,
            "typeText"      => ArticleCategoryModel::TYPE_TEXT[$this->type] ?? "-",
            "createdAt"     => formatDate($this->createdAt->timestamp, "-"),
            "updatedAt"     => formatDate($this->updatedAt->timestamp, "-"),
            "articleDetail" => $this->whenLoaded(
                "articleRel",
                function () {
                    return ArticleListResource::collection([$this->articleRel])->getIterator()->current();
                },
                new MissingValue()
            )
        ];
    }
}
